import {Component, OnInit} from '@angular/core';
import {BrowserUtils} from '../lunhui-web-front-utils/BrowserUtils';
import {Observable} from 'rxjs';
import {HttpQueryParamsDto} from '../lunhui-web-front-dto/HttpQueryParamsDto';
import {environment} from '../../environments/environment';
import {HttpMethodEnum} from '../lunhui-web-front-utils/HttpMethodEnum';
import {HttpUtil} from '../lunhui-web-front-utils/HttpUtil';
import {ResponseBaseDto} from '../lunhui-web-front-dto/ResponseBaseDto';

@Component({
    selector: 'app-lunhui-web-front-home',
    templateUrl: './lunhui-web-front-home.component.html',
    styleUrls: ['./lunhui-web-front-home.component.scss']
})
export class LunhuiWebFrontHomeComponent implements OnInit {

    private roseId: number;

    constructor() {
    }

    ngOnInit() {
        const result: boolean = BrowserUtils.checkToken();

        if (!result) {
            BrowserUtils.backToTarget('login');
            return;
        }

        this.getHomeBaseInfo();
    }

    private async getHomeBaseInfo(): Promise<void> {
        await this.getRoseLocationInfo();
    }

    private async getRoseLocationInfo(): Promise<void> {
        try {
            const cookie = JSON.parse(BrowserUtils.getCookie('token'));
            const roseId = JSON.parse(BrowserUtils.getCookie('roseId'));

            const httpQueryParamsDto: HttpQueryParamsDto = new HttpQueryParamsDto();
            httpQueryParamsDto.url = `${environment.host}rose/RoseLocation?roseId=`;
            httpQueryParamsDto.url += `${roseId}&token=${cookie.token}`;
            httpQueryParamsDto.method = HttpMethodEnum.GET;

            const resultData = await HttpUtil.query(httpQueryParamsDto) as ResponseBaseDto;

            if (resultData.success) {
                console.log(resultData);
            } else {
                HttpUtil.queryError(resultData.errorMessage);
            }
        } catch (e) {
            console.error(e.message);
        }
    }


}
